package edu.bit.water.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.bit.water.service.EntitySensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;

import edu.bit.water.entity.Chart;
import edu.bit.water.service.ChartService;


/**
 * 流程图控制层
 *
 * @author HJS
 */
@Controller
@RequestMapping("/chart")
public class ChartController {

    @Autowired
    ChartService chartService;

    @Autowired
    private EntitySensorService entitySensorService;

    /**
     * @return 返回设备名称
     */
    @RequestMapping(value = "/selectOneName", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, String> selectOneName(@RequestParam("businessId") String businessId,@RequestParam("sensorRowId") String sensorRowId) {
        String s = entitySensorService.querySensorName(businessId,sensorRowId);
        Map<String, String> map = new HashMap<>();
        if (s != null)
            map.put("name", s);
        return map;
    }

    /**
     * 增加流程图相关数据的方法
     *
     * @param jsonObject 流程图实体类
     * @return 返回操作信息
     */
    @ResponseBody
    @RequestMapping("/insert")
    public Map<String, String> insert(@RequestBody JSONObject jsonObject) {
        Chart chart = new Chart();
        chart.setChartName(jsonObject.getString("chartName"));
        chart.setData(jsonObject.getString("data"));
        chart.setBusinessId(Integer.parseInt(jsonObject.getString("businessId")));
        return chartService.addChart(chart);
    }

    /**
     * 删除流程图的方法
     *
     * @param idList 将要被删除id的list集合
     * @return 返回操作信息
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map<String, String> delete(@RequestBody List<Integer> idList) {
        int n = chartService.deleteChart(idList);
        Map<String, String> map = new HashMap<>();
        if (n == 1)
            map.put("status", "success");
        else
            map.put("status", "failed");
        return map;
    }

    /**
     * 修改某个流程图数据
     *
     * @param jsonObject 将要被修改的流程图数据
     * @return 返回操作信息
     */
    @ResponseBody
    @RequestMapping("/update")
    public Map<String, String> update(@RequestBody JSONObject jsonObject) {
        Chart chart = new Chart();
        chart.setId(jsonObject.getIntValue("id"));
        chart.setChartName(jsonObject.getString("chartName"));
        chart.setData(jsonObject.getString("data"));
        return chartService.updateChart(chart);
    }

    /**
     * 查询所有流程图数据的方法
     *
     * @return 成功返回Chart集合, 失败返回NULL
     */
    @ResponseBody
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public Map<String, Object> select() {
        List<Chart> ls = chartService.selectChart();
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("rows", ls);
        return modelMap;
    }

    @ResponseBody
    @RequestMapping(value = "select_by_businessid", method = RequestMethod.GET)
    public Map<String, Object> selectAll(String company_id) {
        List<Chart> ls = chartService.selectChartByBusiness(Integer.parseInt(company_id));
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("rows", ls);
        return modelMap;
    }

    /**
     * 根据ID查询流程图信息的方法
     *
     * @return 成功返回Chart集合, 失败返回NULL
     */
    @ResponseBody
    @RequestMapping("/selectOne")
    public Map<String, Object> selectOne(int id) {
        Chart chart = chartService.selectOne(id);
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("result", chart);
        return modelMap;
    }

    /**
     * 根据传感器名称返回最新水信息记录
     *
     * @param name 传感器名称
     * @return 成功返回最新的水信息, 失败返回NULL
     */
    @ResponseBody
    @RequestMapping("/selectWater")
    public Map<String, Object> selectWater(String name) {
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> map = chartService.selectWater(name);
        if (map == null) {
            modelMap.put("state", "failed");
            modelMap.put("msg", "此传感器不存在相关水信息");
            return modelMap;
        }
        //格式化时间
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        map.replace("创建时间", ft.format(map.get("创建时间")));
        modelMap.put("result", map);
        return modelMap;
    }

    /**
     * 查询所有设备信息方法
     * @param companyId
     * @return 成功返回设备信息集合, 失败返回NULL
     */
    @ResponseBody
    @RequestMapping("/selectSensor")
    public Map<String, Object> selectSensor(@RequestParam("companyId") String companyId) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Map<String, Object>> list = chartService.sensorNameMatch(Integer.parseInt(companyId));
        modelMap.put("results", list);
        return modelMap;
    }


    /**
     * 查询所有所在位置
     * @param businessId
     * @return 成功返回设备信息集合, 失败返回NULL
     */
    @ResponseBody
    @RequestMapping("/queryLocationByBusinessId")
    public Map<String, Object> selectLocationByBusinessId(@RequestParam("businessId") String businessId) {
        return chartService.queryLocationByBusinessId(businessId);
    }

    /**
     * 根据所在位置查询设备类型
     * @param businessId, location
     * @return 成功返回设备信息集合, 失败返回NULL
     */
    @ResponseBody
    @RequestMapping("/querySensorTypeByLocation")
    public Map<String, Object> querySensorTypeByLocation(@RequestParam("businessId") String businessId,
                                                         @RequestParam("location") String location) {
        return chartService.querySensorTypeByLocation(businessId, location);
    }

    /**
     * 根据所在位置查询设备类型
     * @param businessId, location
     * @return 成功返回设备信息集合, 失败返回NULL
     */
    @ResponseBody
    @RequestMapping("/querySensorIdByLocationAndType")
    public Map<String, Object> querySensorIdByLocationAndType(@RequestParam("businessId") String businessId,
                                                         @RequestParam("location") String location,
                                                              @RequestParam("sensorType") String sensorType) {
        return chartService.querySensorIdByLocationAndType(businessId, location, sensorType);
    }


    /**
     * @return 从设备元数据表中返回所有属性值
     */
    @ResponseBody
    @RequestMapping("/selectWaterCsName")
    public Map<String, Object> selectWaterCsName() {
        Map<String, Object> modelMap = new HashMap<>();
        List<String> csNameLists = chartService.WatercsName();
        modelMap.put("results", csNameLists);
        return modelMap;
    }

    @ResponseBody
    @RequestMapping("/selectByProperty")
    public Map<String, Object> selectByProperty(@RequestBody List<String> propertyList) {
        Map<String, Object> modelMap = new HashMap<>();
        int id = Integer.parseInt(propertyList.get(0));
        propertyList.remove(0);
        List<Map<String, Object>> list = chartService.selectByProperty(id, propertyList);
        modelMap.put("results", list);
        return modelMap;
    }

    /**
     * 查询所有所在位置
     * @param businessId
     * @return 成功返回设备信息集合, 失败返回NULL
     */
    @ResponseBody
    @RequestMapping("/querySensorNameByBusinessId")
    public Map<String, Object> querySensorNameByBusinessId(@RequestParam("businessId") String businessId) {
        return chartService.querySensorNameByBusinessId(businessId);
    }

    /**
     * 查询所有所在位置
     * @param businessId
     * @return 成功返回设备信息集合, 失败返回NULL
     */
    @ResponseBody
    @RequestMapping("/querySensorNoByName")
    public Map<String, Object> querySensorNoByName(@RequestParam("businessId") String businessId,@RequestParam("name") String name) {
        return chartService.querySensorNoByName(businessId,name);
    }

    /**
     * 查询所有设备信息方法
     * @param sensorId
     * @return 成功返回设备信息集合, 失败返回NULL
     */
    @ResponseBody
    @RequestMapping("/selectSensorBySensorId")
    public Map<String, Object> selectSensorBySensorId(@RequestParam("businessId") String businessId,@RequestParam("sensorId") String sensorId) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Map<String, Object>> list = chartService.sensorNameMatchBySensorId(businessId,sensorId);
        modelMap.put("results", list);
        return modelMap;
    }
}
